package com.gis.servlet.notice;

import cn.hutool.core.date.DateUtil;
import com.gis.bean.Notice;
import com.gis.dao.NoticeDao;
import com.gis.util.Constant;
import com.gis.util.ResponseUtil;
import com.gis.util.RestResponse;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author t-zhaoheng
 * @since 2022/4/19
 */
@WebServlet("/notice/update.do")
public class UpdateNoticeServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
   this.doPost(req, resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    Integer id = Integer.parseInt(req.getParameter("id"));
    String title = req.getParameter("title");
    String content = req.getParameter("content");
    String image = req.getParameter("fileName");

    Notice notice = new Notice();
    notice.setId(id);
    notice.setContent(content);
    notice.setTitle(title);
    notice.setImage(image);

    int i = NoticeDao.getInstance().updateNotice(notice);
    if (i > 0) {
      ResponseUtil.writeJson(resp, RestResponse.success(Constant.UPDATE_SUCCESS));
    } else {
      ResponseUtil.writeJson(resp, RestResponse.fail(Constant.UPDATE_FAILED));
    }
  }
}
